package com.zerdoor.controller;

import com.zerdoor.model.UserInfo;
import com.zerdoor.service.ITaskSigningService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by zhongjieyu on 2014/10/29.
 * 任务签约 控制层
 */
@Controller
@RequestMapping("/taskSigning")
public class TaskSigningController {

    @Autowired
    private ITaskSigningService taskSigningService;

    /**
     * 跳转到 委托方 任务签约页面
     * @param request
     * @param itemId
     * @param type 如果type为0表示委托方，1表示艺术家
     * @return
     */
    @RequestMapping(value = "/taskSigning", method = RequestMethod.GET)
    public String taskSigning(HttpServletRequest request,int itemId,int type){

        try {
            UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
            request.setAttribute("contractNames",taskSigningService.findContractNameByItemId(itemId,userInfo.getUserId(),type));

            if(type == 0){

                request.setAttribute("handleUrl","baileContract");
            }else{
                request.setAttribute("handleUrl","artistContract");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return "/taskSigning/taskSigningBaile";
    }


    /**
     * 跳转到 委托方 合同页面
     * @param request
     * @param contractId
     * @return
     */
    @RequestMapping(value = "/baileContract", method = RequestMethod.GET)
    public String baileContract(HttpServletRequest request,int contractId){

        try {

            request.setAttribute("contract",taskSigningService.findContractByContractId(contractId));

        } catch (Exception e) {
            e.printStackTrace();
        }

        return "/taskSigning/baileContract";
    }

    /**
     * 跳转到 艺术家 合同页面
     * @param request
     * @param contractId
     * @return
     */
    @RequestMapping(value = "/artistContract", method = RequestMethod.GET)
    public String artistContract(HttpServletRequest request,int contractId){

        try {

            request.setAttribute("contract",taskSigningService.findContractByContractId(contractId));

        } catch (Exception e) {
            e.printStackTrace();
        }

        return "/taskSigning/artistContract";
    }

    /**
     *
     *
     * @param contractId 合同ID
     * @return
     */
    @RequestMapping(value = "/findContractByContractId", method = RequestMethod.POST)
    public ResponseEntity<?> findContractByContractId(int contractId) {

        try {
            Map<String,Object> list =  taskSigningService.findContractByContractId(contractId);

            ResponseEntity<Map<String,Object>> responseEntity = new ResponseEntity<Map<String,Object>>(list, HttpStatus.OK);
            return responseEntity;
        } catch (Exception e) {

            e.printStackTrace();

            ResponseEntity<String> responseEntity = new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
            return responseEntity;
        }

    }


    /**
     * 保存合同信息数据
     * @param param 合同信息内容
     * @return
     */
    @RequestMapping(value = "/saveContract", method = RequestMethod.POST)
    public ResponseEntity<?> saveContract(@RequestParam Map param) {

        try {
            taskSigningService.updateContractByContractId(param);

            ResponseEntity<String> responseEntity = new ResponseEntity<String>("success", HttpStatus.OK);
            return responseEntity;
        } catch (Exception e) {

            e.printStackTrace();

            ResponseEntity<String> responseEntity = new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
            return responseEntity;
        }

    }


    /**
     * 委托方确认
     * @param contractId 合同的ID
     * @return
     */
    @RequestMapping(value = "/baileSureContract", method = RequestMethod.POST)
    public ResponseEntity<?> baileSureContract(int contractId) {

        try {
            taskSigningService.updateBaileSureContractByContractId(contractId);

            ResponseEntity<String> responseEntity = new ResponseEntity<String>("success", HttpStatus.OK);
            return responseEntity;
        } catch (Exception e) {

            e.printStackTrace();

            ResponseEntity<String> responseEntity = new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
            return responseEntity;
        }

    }


    /**
     * 艺术家确认
     * @param contractId 合同的ID
     * @return
     */
    @RequestMapping(value = "/artistSureContract", method = RequestMethod.POST)
    public ResponseEntity<?> artistSureContract(int contractId) {

        try {
            taskSigningService.updateArtistSureContractByContractId(contractId);

            ResponseEntity<String> responseEntity = new ResponseEntity<String>("success", HttpStatus.OK);
            return responseEntity;
        } catch (Exception e) {

            e.printStackTrace();

            ResponseEntity<String> responseEntity = new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
            return responseEntity;
        }

    }



}
